#include "Section.h"


Section::Section(std::string name, unsigned int priority, Node * parent)
{
	this->_name = name;
	this->_priority = priority;
	this->_parent = parent;
}

void Section::print(std::string& tree)
{
	tree.append(this->_name);
	for (unsigned int i=0; i < this->countChildren(); i++)
	{
		tree.append("(");
		this->getChild(i)->print(tree);
		tree.append(")");
	}
}

void Section::clone(Node** node)
{
	(*node) = new Section(this->_name, this->_priority, this->_parent);
	if (this->countChildren() > 0)
	{
		for (unsigned int i=0; i < this->countChildren(); i++)
		{
			(*node)->addChild((*node));
			Node* ch = (*node)->getChild(0);
			this->getChild(i)->clone(&ch);
		}
	}
}

Section::~Section(void)
{
}

int Section::getType() const
{
	return SECTION;
}

bool Section::checkNode(std::vector<Variables> & vars, const Node* goal)
{
	bool ismod = false;
	if(this->getName().compare("root") == 0)
	{
		std::vector<Variables> clone;
		for(unsigned int i=0;i<vars.size();i++)
		{
			clone.push_back(vars[i]);
		}
		if(this->getChild(1)->checkNode(clone, goal))
		{
			if(vars.size() != clone.size())
			{
				vars.clear();
				for(unsigned int i=0;i<clone.size();i++)
				{
					vars.push_back(clone[i]);
				}
			}
			return true;
		}
		else
		{
			return false;
		}
	}
	else if(this->getName().compare("clauses") == 0)
	{
		bool isChecked = false;
		int n = vars.size();
		for(unsigned int i=0;i<this->countChildren();i++)
		{
			std::vector<Variables> clone;
			for(unsigned int j=0;j<n;j++)
			{
				clone.push_back(vars[j]);
			}
			if(this->getChild(i)->checkNode(clone, goal))
			{
				isChecked = true;
				bool ismod = false;
				for(unsigned int j=0;j<vars.size();j++)
				{
					if(!(vars[j] == clone[j]))
					{
						ismod = true;
						break;
					}
				}
				if(ismod)
				{
					for(unsigned int j=0;j<clone.size();j++)
					{
						vars.push_back(clone[j]);
					}
				}
			}
		}
		return isChecked;
	}
	return false;
}

std::string Section::getValue(std::vector<Variables>)
{
	return "";
}
